package com.xiaozou.service;

import cn.hutool.core.io.FileUtil;
import com.xiaozou.enums.TuringIndustry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import cn.hutool.core.util.CharsetUtil;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class IndustryAskMethodLoader {

    private static final Map<TuringIndustry, List<String>> IndustryAskMethodMap = new HashMap<>();

    @Autowired
    private ResourceLoader resourceLoader;


    @PostConstruct
    public void init() {
        for (TuringIndustry industry : TuringIndustry.values()) {
            String code = industry.getCode();
            String path = String.format("classpath:industry_phrases/%s.txt", code);
            log.info("尝试加载路径: {}", path);

            try {
                Resource resource = resourceLoader.getResource(path);
                if (resource.exists()) { // 检查资源是否存在
                    List<String> lines = FileUtil.readLines(resource.getFile(), CharsetUtil.CHARSET_UTF_8);
                    IndustryAskMethodMap.put(industry, lines.stream().map(line -> line.replaceAll("-", "")).map(String::trim).collect(Collectors.toList()));
                } else {
                    log.warn("文件路径不存在: {}", path);
                }
            } catch (Exception e) {
                log.error("加载行业问法失败，路径: {}", path, e);
            }
        }
        log.info("行业问法加载完成: {}", IndustryAskMethodMap.keySet());
        log.info("行业问法数量: {}", IndustryAskMethodMap.values().stream().mapToInt(List::size).sum());
    }


    public List<String> getAskMethods(TuringIndustry industry) {
        return IndustryAskMethodMap.get(industry);
    }
}


